<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of MenuDaoImpl
 *
 * @author krisada.thiangtham
 */
require '../com.swiss.mooh.dao/MenuDao.php';
require '../com.swiss.mooh.utils/ArrayList.php';

class MenuDaoImpl implements MenuDao {

    public function __construct() {
        
    }

    public function createNewMenu($menuTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("INSERT INTO SWISS_MENU VALUE (?,?,?)");
        $stmt->bind_param('sss', $menuTO->getMenuID(), $menuTO->getMenuName(), $menuTO->getMenuCreatedDateTime());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteMenu($menuID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_MENU WHERE MENU_ID LIKE ?");
        $stmt->bind_param('s', $menuID);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function updateMenu($menuTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("UPDATE SWISS_MENU SET MENU_NAME = ? WHERE MENU_ID LIKE ?");
        $stmt->bind_param('ss', $menuTO->getMenuName(), $menuTO->getMenuID());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function createNewMenuLevel1($menuTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("INSERT INTO SWISS_MENU_LEVEL1 VALUE (?,?,?,?,?)");
        $stmt->bind_param('sssss', $menuTO->getMenuID(), $menuTO->getMenuName(), $menuTO->getMenuCreatedDateTime(), $menuTO->getContentPage(), $menuTO->getRefID());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function createNewMenuLevel2($menuTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("INSERT INTO SWISS_MENU_LEVEL2 VALUE (?,?,?,?,?)");
        $stmt->bind_param('sssss', $menuTO->getMenuID(), $menuTO->getMenuName(), $menuTO->getMenuCreatedDateTime(), $menuTO->getContentPage(), $menuTO->getRefID());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteMenuLevel1($menuID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_MENU_LEVEL1 WHERE MENU_ID LIKE ?");
        $stmt->bind_param('s', $menuID);
        if ($stmt->execute()) {
            $stmt->close();

            $objConnect = mysql_connect($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword']) or die(mysql_error());
            $objDB = mysql_select_db($conf['dataBaseName']) or die("Cannot Connect Database");
            mysql_query('utf-8', $objConnect);

            $sqlDelete = mysql_query("DELETE FROM SWISS_MENU_LEVEL2 WHERE REF_ID LIKE '" . $menuID . "'");
            if ($sqlDelete) {
                return 200;
            }
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function deleteMenuLevel2($menuID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("DELETE FROM SWISS_MENU_LEVEL2 WHERE MENU_ID LIKE ?");
        $stmt->bind_param('s', $menuID);
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function updateMenuLevel1($menuTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("UPDATE SWISS_MENU_LEVEL1 SET MENU_NAME = ? WHERE MENU_ID LIKE ?");
        $stmt->bind_param('ss', $menuTO->getMenuName(), $menuTO->getMenuID());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function updateMenuLevel2($menuTO) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $stmt = $mysqli->prepare("UPDATE SWISS_MENU_LEVEL2 SET MENU_NAME = ? WHERE MENU_ID LIKE ?");
        $stmt->bind_param('ss', $menuTO->getMenuName(), $menuTO->getMenuID());
        if ($stmt->execute()) {
            $stmt->close();
            return 200;
        } else {
            return mysqli_error($mysqli);
        }
    }

    public function getAllMenu() {
//        require '../com.swiss.mooh.model/Menu.php';
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_MENU ORDER BY CREATED_DATE_TIME ASC;");
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $menuVO = new Menu();
            $menuVO->setMenuID($x['MENU_ID']);
            $menuVO->setMenuName($x['MENU_NAME']);
            $menuVO->setMenuCreatedDateTime($x['CREATED_DATE_TIME']);

            $detailList->Add($menuVO);
        }
        $stmt->close();
        return $detailList;
    }

    public function getAllMenuLevelOneByMainMenuID($mainMenuID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_MENU_LEVEL1 WHERE REF_ID LIKE ? ORDER BY CREATED_DATE_TIME ASC;");
        $stmt->bind_param('s', $mainMenuID);
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $menuVO = new Menu();
            $menuVO->setMenuID($x['MENU_ID']);
            $menuVO->setMenuName($x['MENU_NAME']);
            $menuVO->setMenuCreatedDateTime($x['CREATED_DATE_TIME']);
            $menuVO->setContentPage($x['CONTENT_PAGE_ID']);
            $menuVO->setRefID($x['REF_ID']);

            $detailList->Add($menuVO);
        }
        $stmt->close();
        return $detailList;
    }

    public function getAllMenuLevelTwoByMainMenuID($mainMenuID) {
        $conf = parse_ini_file("../../model-db-connection/configuration.ini");
        $mysqli = new mysqli($conf['dataBaseDomain'], $conf['dataBaseUsername'], $conf['dataBasePassword'], $conf['dataBaseName']);
        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }
        $detailList = new ArrayList();
        $stmt = $mysqli->prepare("SELECT * FROM SWISS_MENU_LEVEL2 WHERE REF_ID LIKE ? ORDER BY CREATED_DATE_TIME ASC;");
        $stmt->bind_param('s', $mainMenuID);
        $stmt->execute();
        $meta = $stmt->result_metadata();
        while ($field = $meta->fetch_field()) {
            $parameters[] = &$row[$field->name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $parameters);
        while ($stmt->fetch()) {
            foreach ($row as $key => $val) {
                $x[$key] = $val;
            }
            $menuVO = new Menu();
            $menuVO->setMenuID($x['MENU_ID']);
            $menuVO->setMenuName($x['MENU_NAME']);
            $menuVO->setMenuCreatedDateTime($x['CREATED_DATE_TIME']);
            $menuVO->setContentPage($x['CONTENT_PAGE_ID']);
            $menuVO->setRefID($x['REF_ID']);

            $detailList->Add($menuVO);
        }
        $stmt->close();
        return $detailList;
    }

}
